package com.lanou3g.demo.service.impl;

import com.lanou3g.demo.controller.params.AdminParams;
import com.lanou3g.demo.entity.AdminInfo;
import com.lanou3g.demo.entity.AdminRole;
import com.lanou3g.demo.entity.impl.AdminInfoImpl;
import com.lanou3g.demo.mapper.AdminInfoMapper;
import com.lanou3g.demo.service.IAdminInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminInfoService implements IAdminInfoService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    /**
     * 查看个人信息
     *
     * @param adminInfo
     * @return
     */
    @Override
    public AdminInfo selectAdminInfoWithRoleName(AdminInfo adminInfo) {
        return adminInfoMapper.selectAdminInfoWithRoleName(adminInfo);
    }

    /**
     * 修改个人信息 姓名,电话,Email
     *
     * @param adminInfo
     * @return
     */
    @Transactional
    @Override
    public boolean updateAdminInfo(AdminInfo adminInfo) {
        int i = adminInfoMapper.updateAdminInfo(adminInfo);
        return i > 0;
    }

    /**
     * 校验原密码
     *
     * @param adminInfo1 从session中拿出来的对象
     * @param adminInfo2
     * @return
     */
    @Override
    public boolean findOne(AdminInfo adminInfo1, AdminInfo adminInfo2) {
        AdminInfo ai = adminInfoMapper.findOne(adminInfo1);
        return ai.getPassword().equals(adminInfo2.getPassword());
    }

    /**
     * 插入新密码
     *
     * @param adminInfo
     * @return
     */
    @Transactional
    @Override
    public boolean updateAdminPwd(AdminInfo adminInfo) {
        int i = adminInfoMapper.updateAdminPwd(adminInfo);
        return i > 0;
    }

    /**
     * 查看所有的管理员
     *
     * @return
     */
    @Override
    public List<AdminInfoImpl> selectAllAdmin() {
        return adminInfoMapper.selectAllAdmin();
    }

    /**
     * 查询管理员
     *
     * @param roleName
     * @return
     */
    @Override
    public List<AdminInfoImpl> selectRoleWithAdmin(String roleName) {
        return adminInfoMapper.selectRoleWithAdmin(roleName);
    }

    /**
     * 添加管理员
     *
     * @param adminParams
     * @return
     */
    @Transactional
    @Override
    public boolean addAdminWithRole(AdminParams adminParams) throws SQLException {
        AdminInfo adminInfo = adminParams.getAdminInfo();
        adminInfoMapper.insertAdminInfo(adminInfo);
        List<AdminRole> adminRoles = new ArrayList<>();
        for (Integer roleId : adminParams.getRoleIds()) {
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(roleId);
            adminRole.setAdminId(adminInfo.getAdminId());
            adminRoles.add(adminRole);
        }
        int i = adminInfoMapper.insertAdminRole(adminRoles);
        return i > 0;
    }

    /**
     * 修改管理员信息回显
     *
     * @param adminInfo
     * @return
     */
    @Override
    public AdminParams modifyFindAdmin(AdminInfoImpl adminInfo) {
        AdminParams adminParams = new AdminParams();

        adminParams.setAdminInfo(adminInfoMapper.modifyFindAdmin(adminInfo));

        List<Integer> roleIds = new ArrayList<>();

        List<AdminRole> adminRoles = adminInfoMapper.modifyFindRole(adminInfo);
        for (AdminRole adminRole : adminRoles) {
            roleIds.add(adminRole.getRoleId());
        }
        adminParams.setRoleIds(roleIds);
        return adminParams;
    }


    /**
     * 修改管理员信息
     *
     * @param adminParams
     * @param adminId
     * @return
     */
    @Transactional
    @Override
    public boolean modifyAdmin(AdminParams adminParams, String adminId) throws SQLException {
        AdminInfo adminInfo = adminParams.getAdminInfo();
        adminInfo.setAdminId(Integer.parseInt(adminId));
        adminInfoMapper.updataAdminId(adminInfo);
        adminInfoMapper.deleteAdminWithRole(adminId);
        List<AdminRole> adminRoles = new ArrayList<>();
        for (Integer rId : adminParams.getRoleIds()) {
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(rId);
            adminRole.setAdminId(Integer.parseInt(adminId));
            adminRoles.add(adminRole);
        }
        int i = adminInfoMapper.insertAdminRole(adminRoles);
        return i > 0;
    }

    /**
     * 删除管理员
     *
     * @param adminId
     * @return
     */
    @Transactional
    @Override
    public boolean deleteAdmin(String adminId) throws SQLException {
        int i = adminInfoMapper.deleteAdmin(adminId);
        if (i > 0) {
            int j = adminInfoMapper.deleteAdminWithRole(adminId);
            return j > 0;
        } else {
            return false;
        }
    }

    /**
     * 密码重置
     *
     * @param adminIds
     * @return
     */
    @Transactional
    @Override
    public int resetPwd(List<String> adminIds) throws SQLException {
        return adminInfoMapper.resetPwd(adminIds);
    }
}
